package com.xxadmin.usercenter.config.security.service;

import com.xxadmin.usercenter.config.security.dto.UserDetailDTO;
import com.xxadmin.usercenter.dto.UserInfoDTO;
import com.xxadmin.usercenter.service.RoleDetailService;
import com.xxadmin.usercenter.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户授权
 *
 * @author:chenming
 * @date:2018/9/4
 */
@Service
public class OauthUserDetailServiceImpl implements UserDetailsService{

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RoleDetailService roleDetailService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfoDTO userInfoDTO = userInfoService.getUserByUserName(username);

        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        list.add(new SimpleGrantedAuthority("app1"));
        list.add(new SimpleGrantedAuthority("app2"));

        UserDetailDTO userDetail = null;
        if(userInfoDTO != null){
            userDetail = new UserDetailDTO(userInfoDTO.getUserName(),userInfoDTO.getPassword(),list);

            userDetail.setChannelCode(userInfoDTO.getChannelCode());
            userDetail.setUserStatus(userInfoDTO.getStatus());

            // 获取用户角色

        }

        return userDetail;
    }

}
